package main

import (
	"context"
	"example.com/m/v2/global"
	"example.com/m/v2/initialize"
	"example.com/m/v2/middleware"
	"example.com/m/v2/router"
	"fmt"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"time"
)

// @securityDefinitions.apikey  ApiKeyAuth
// @in header
// @name Authorization
func main() {
	//basic  BasicAuth

	//ctx01, cancel := context.WithCancel(context.Background())
	//ctx02, cancel := context.WithDeadline(ctx01, time.Now().Add(1*time.Second))
	//
	//defer cancel()
	//
	//for i := 1; i <= 5; i++ {
	//	go monitor(ctx02, i)
	//}
	//
	//time.Sleep(5 * time.Second)
	//if ctx02.Err() != nil {
	//	fmt.Println("监控器取消的原因: ", ctx02.Err())
	//}

	//var wg sync.WaitGroup
	//ch := make(chan string, 2)
	//urls := []string{
	//	"https://www.google.com",
	//	"https://www.bing.com",
	//	"https://www.twitter.com",
	//	"https://www.github.com",
	//	"https://www.reddit.com",
	//	"https://www.cnblogs.com",
	//	"https://www.instagram.com",
	//	"https://www.baidu.com",
	//}
	//client := &http.Client{
	//	Timeout: 2 * time.Second, // 设置超时时间为10秒
	//}
	//wg.Add(len(urls))
	//for _, url := range urls {
	//	go func() {
	//		resp, err := client.Get(url)
	//		if err != nil {
	//			fmt.Println(err)
	//		} else {
	//			defer resp.Body.Close()
	//			fmt.Println(url)
	//		}
	//		wg.Done()
	//	}()
	//}
	//
	//wg.Wait()

	//fmt.Println(html)

	//
	//for c := range ch {
	//	fmt.Println(c)
	//}
	////go PrintHtml(ch)
	//fmt.Println("Done")

	gin.SetMode(gin.DebugMode)
	initialize.Viper()

	fmt.Println(global.GVA_CONFIG.JWT.SigningKey)

	global.GVA_DB = initialize.Gorm()

	var logger *zap.Logger
	logConfig := middleware.LogConfig{
		Level:      "debug",
		Filename:   "log/debug.log",
		MaxSize:    1,
		MaxAge:     30,
		MaxBackups: 100,
	}
	initialize.InitLogger(&logConfig, &logger)

	r := gin.New()
	corsConfig := cors.DefaultConfig()
	corsConfig.AllowOrigins = []string{"*"}                                                         // 允许所有域名跨域访问
	corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "DELETE", "PATCH"}                     // 允许的方法
	corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Authorization"} // 允许的头部信息
	r.Use(cors.New(corsConfig))
	r.Use(middleware.GinLogger(logger), middleware.GlobalErrorMiddleware(logger, true))
	router.APIRouter(r)
	r.Run(":8080")
}

func GetHtml(url string, ch chan<- string) {
	//fmt.Println(url)
	client := &http.Client{
		Timeout: 2 * time.Second, // 设置超时时间为10秒
	}
	response, err := client.Get(url)
	if err != nil {
		//fmt.Println("error:", err)
		//return "", err
	} else {
		defer response.Body.Close()
		//html, _ := io.ReadAll(response.Body)
		ch <- url
		//fmt.Println(string(html))
		//fmt.Println(url)

		//return url, nil
	}
}

func PrintHtml(c <-chan string) {
	//time.Sleep(3 * time.Second)
	url := <-c
	fmt.Println(url)
}

func AddSliceItem(s []int, item int) {
	fmt.Println(s)
	s[0] = 100
	s = append(s, item)
	fmt.Println(s)
}

func monitor(ctx context.Context, number int) {
	for {
		select {
		case v := <-ctx.Done():
			fmt.Printf("监控器%v，接收到通道值为：%v，监控结束。\n", number, v)
			return
		default:
			fmt.Printf("监控器%v，正在监控中...\n", number)
			time.Sleep(2 * time.Second)
		}
	}
}
